package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.AccountService;

import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/catalog")
public class CatalogController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AccountService accountService;


    @GetMapping("/view")
    public String view() {
        return "catalog/main";
    }

    @GetMapping("/login")
    public String login(){
        return "catalog/login";
    }

    @PostMapping("/loginConfirm")
    public String loginConfirm(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Account account = accountService.getAccountByUserId(username);
        if (password.equals(account.getPassword())) {
            return "catalog/main";
        } else {
            return "catalog/login";
        }
    }

    @RequestMapping("/sinaLoginRedirect")
    public String sinaLoginRedirect(HttpServletRequest request,HttpServletResponse response) {
        return "redirect:/catalog/view";
    }

    @GetMapping("/category")
    public String viewCategory(Model model){
        List<Category> categoryList = categoryService.getCategoryList();
        ////System.out.println("categoryList===="+categoryList.toString());
        model.addAttribute("categoryList",categoryList);
        return "catalog/category";
    }

    @GetMapping("/viewProducts")
    public String viewProducts(String categoryId,Model model){
        List<Product> productList = categoryService.getProductListByCategoryId(categoryId);
        ////System.out.println("productList====="+productList.toString());
        model.addAttribute("categoryId",categoryId);
        model.addAttribute("productList",productList);
        return "catalog/viewProduct";
    }

    @GetMapping("/viewItems")
    public String viewItems(String productId,Model model){
        List<Item> itemList = categoryService.getItemListByProductId(productId);
        Product product = categoryService.getProductByProductId(productId);
        ////System.out.println("productId=="+productId+"productN=="+product.getName()+"itemList====="+itemList.toString());
        model.addAttribute("product",product);
        model.addAttribute("productId",productId);
        model.addAttribute("itemList",itemList);
        return "catalog/viewItems";
    }

    @GetMapping("/newCategoryForm")//跳转添加Category界面
    public String newCategoryForm(){
        return "catalog/newCategoryForm";
    }

    @GetMapping("/newProductForm")//跳转新增product界面
    public String  newProductForm(String categoryId, Model model){
        model.addAttribute("categoryId",categoryId);
        ////System.out.println("跳转newProductForm with categoryId:==" + categoryId);
        return "catalog/newProductForm";
    }

    @PostMapping("/newCategory")//添加Category，回到显示Category分类界面
    public String newCategory(Category category,Model model){
        categoryService.newCategory(category);
        List<Category> categoryList = categoryService.getCategoryList();
        model.addAttribute("categoryList",categoryList);
        ////System.out.println("新增Category,,,,"+category.getCategoryId()+"   "+category.getName());
        return "catalog/category";
    }

    @GetMapping("/deleteCategory")
    public String deleteCategory(String categoryId, Model model){//删除category
        categoryService.deleteCategory(categoryId);
        List<Category> categoryList = categoryService.getCategoryList();
        model.addAttribute("categoryList",categoryList);
        ////System.out.println("删除Category,,,,"+categoryId);
        return "catalog/category";
    }

    @PostMapping("/newProduct")//新增product
    public String newProduct(Product product, String CATEGORYID,  Model model){//CATEGORYID大写防止参数混乱？？？
        //String categoryId = product.getCategoryId();
        categoryService.newProduct(product);
        ////System.out.println("categoryId=="+CATEGORYID+"   ProductId=="+product.getCategoryId()+"    productName=="+product.getName());
        return viewProducts(CATEGORYID,model);
    }

    @GetMapping("/deleteProduct")//删除product
    public String deleteProduct(String productId, Model model){
        Product product = categoryService.getProductByProductId(productId);
        //System.out.println("CategoryId===="+model.getAttribute("categoryId"));
        categoryService.deleteProduct(productId);
        //System.out.println("删除Product,,,,"+productId);
        return viewProducts(productId,model);
    }

    @GetMapping("/newItemForm")//新增Item表单
    public String newItemForm(String productId, Model model){
        model.addAttribute("productId",productId);
        //System.out.println("跳转newItemForm with productId:==" + productId);
        return "catalog/newItemForm";
    }

    @GetMapping("/editItemForm")//修改Item表单
    public String editItemForm(String itemId, Model model){
        Item item = categoryService.getItem(itemId);
        model.addAttribute("item",item);
        //System.out.println("跳转editItemForm with itemId:==" + item.getItemId()+"  PRODUCTID=="+item.getProductId());
        return "catalog/editItemForm";
    }

    @PostMapping("/newItem")//新增Item
    public String newItem(Item item, String PRODUCTID, Model model){
        categoryService.newItem(item);
        categoryService.newInventory(item);
        //System.out.println("PRODUCTID=="+PRODUCTID+"   ItemId=="+item.getItemId());
        return viewItems(PRODUCTID,model);
    }

    @PostMapping("/editItem")
    public String editItem(Item item, Model model){
        String productId = item.getProductId();
        categoryService.updateItem(item);
        //System.out.println("editItem Id = "+item.getItemId()+"    ProductId = "+productId);
        return viewItems(productId,model);
    }

    @GetMapping("/deleteItem")
    public String deleteItem(String itemId, Model model){
        Item item = categoryService.getItem(itemId);
        categoryService.deleteItem(itemId);
        //System.out.println("删除Item==="+itemId);
        return viewItems(item.getProductId(),model);
    }

    @GetMapping("/categoryExist")
    public void categoryIsExist(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String categoryId = request.getParameter("categoryId");
        Category category = categoryService.getCategoryById(categoryId);
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();
        if(category != null){
            out.print("Exist");
            //System.out.println(categoryId+"Exist");
        }else if(categoryId=="" || categoryId == null){
            out.print("NULL");
            //System.out.println("NULL");
        }else{
            out.print("Not Exist");
            //System.out.println(categoryId+"Not Exist");
        }
        out.flush();
        out.close();
    }

}
